package com.lyc.mall.controller.mall;

import com.google.gson.Gson;
import com.lyc.mall.model.PO.Type;
import com.lyc.mall.service.GoodsService;
import com.lyc.mall.service.impl.GoodsServiceImpl;
import com.lyc.mall.util.HttpUtils;
import com.lyc.mall.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/mall/index/*")
public class IndexServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();
    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = HttpUtils.getOperation(request);
        if ("getType".equals(operation)) {
            getType(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    /**
     * 1、获取请求参数
     * 2、执行业务逻辑
     * 3、返回响应
     *
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> type = goodsService.getType();
        response.getWriter().println(gson.toJson(Result.OK(type)));
    }
}
